package com.timmy.chi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.timmy.chi.R;
import com.timmy.chi.model.beans.Foods;

import java.util.ArrayList;
import java.util.List;

public class FoodsAdapter extends BaseAdapter {


    private final Context mContext;

    private List<Foods.Order.FoodStatesBean> mData = new ArrayList<>();

    public FoodsAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Foods.Order.FoodStatesBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final Viewholder viewholder;

        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.foods_item, null);
            viewholder = new Viewholder();

            viewholder.tv_name = (TextView) view.findViewById(R.id.foods_item_name);
            viewholder.tv_material = (TextView) view.findViewById(R.id.foods_item_count);
            viewholder.tv_price = (TextView) view.findViewById(R.id.foods_item_price);
            viewholder.iv_image = (ImageView) view.findViewById(R.id.foods_item_image);
            viewholder.tv_state = (TextView) view.findViewById(R.id.foods_item_state);
            view.setTag(viewholder);

        } else {
            viewholder = (Viewholder) view.getTag();
        }

        Foods.Order.FoodStatesBean bean = mData.get(position);
        Foods.Order.FoodStatesBean.FoodBean food = bean.getFood();

        int sid = bean.getSid();

        if (sid == 1) {
            viewholder.tv_state.setText("尚未烹饪");
        } else if(sid == 2) {
            viewholder.tv_state.setText("已烹饪");
        }else if(sid == 3) {
            viewholder.tv_state.setText("该订单已撤销");
        }

        int count = bean.getCount();

        String name = food.getName();
        String image = food.getImage();
        float price = (float) food.getPrice();


        viewholder.tv_name.setText(name);
        viewholder.tv_material.setText("x" + count);
        viewholder.tv_price.setText("" + price);

        Glide.with(mContext)
                .load(image)
                .into(viewholder.iv_image);
        return view;
    }

    public void setData(List<Foods.Order.FoodStatesBean> data) {
        mData.clear();
        mData = data;
        notifyDataSetChanged();
    }

    class Viewholder {
        TextView tv_name;
        TextView tv_state;
        TextView tv_material;
        TextView tv_price;
        ImageView iv_image;
    }
}
